package com.yic.module.system.service.code;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.pojo.PageResult;
import com.yic.module.system.controller.admin.code.vo.CodeRuleCreateReqVO;
import com.yic.module.system.controller.admin.code.vo.CodeRuleExportReqVO;
import com.yic.module.system.controller.admin.code.vo.CodeRulePageReqVO;
import com.yic.module.system.controller.admin.code.vo.CodeRuleUpdateReqVO;
import com.yic.module.system.convert.code.CodeRuleConvert;
import com.yic.module.system.dal.dataobject.code.CodeRuleDO;
import com.yic.module.system.dal.mysql.code.CodeRuleMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.system.enums.ErrorCodeConstants.*;
import static com.yic.module.system.enums.code.CodeRuleTypeEnum.SERIAL;

/**
 * 编号规则明细 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class CodeRuleServiceImpl implements CodeRuleService {

    @Resource
    private CodeRuleMapper codeRuleMapper;

    @Override
    public Long createCodeRule(CodeRuleCreateReqVO createReqVO) {
        // 插入
        CodeRuleDO codeRule = CodeRuleConvert.INSTANCE.convert(createReqVO);
        codeRuleMapper.insert(codeRule);
        // 返回
        return codeRule.getId();
    }

    @Override
    public void batchCreateCodeRule(List<CodeRuleCreateReqVO> createReqVOS) {
        List<CodeRuleDO> codeRules = CodeRuleConvert.INSTANCE.convertList03(createReqVOS);
        codeRuleMapper.insertBatch(codeRules);
    }

    @Override
    public void updateCodeRule(CodeRuleUpdateReqVO updateReqVO) {
        // 校验存在
        validateCodeRuleExists(updateReqVO.getId());
        // 更新
        CodeRuleDO updateObj = CodeRuleConvert.INSTANCE.convert(updateReqVO);
        codeRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteCodeRule(Long id) {
        // 校验存在
        validateCodeRuleExists(id);
        // 删除
        codeRuleMapper.deleteById(id);
    }

    @Override
    public void deleteCodeRuleByCodeId(Long codeId) {
        codeRuleMapper.deleteByCodeId(codeId);
    }

    private void validateCodeRuleExists(Long id) {
        if (codeRuleMapper.selectById(id) == null) {
            throw exception(CODE_RULE_NOT_EXISTS);
        }
    }

    @Override
    public CodeRuleDO getCodeRule(Long id) {
        return codeRuleMapper.selectById(id);
    }

    @Override
    public List<CodeRuleDO> getCodeRuleList(Collection<Long> ids) {
        return codeRuleMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CodeRuleDO> getCodeRulePage(CodeRulePageReqVO pageReqVO) {
        return codeRuleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CodeRuleDO> getCodeRuleList(CodeRuleExportReqVO exportReqVO) {
        return codeRuleMapper.selectList(exportReqVO);
    }

    @Override
    public void validateCodeRule(List<CodeRuleCreateReqVO> codeRuleCreateReqVOS) {
        if (ObjectUtil.isEmpty(codeRuleCreateReqVOS)){
            throw exception(CODE_RULE_NOT_EMPTY);
        }
        boolean exists = codeRuleCreateReqVOS.stream().filter(reqVO -> reqVO.getType().equals(SERIAL.getType())).findAny().isPresent();
        if (!exists) {
            throw exception(CODE_RULE_NOT_SERIAL);
        }
    }

    @Override
    public List<CodeRuleDO> getCodeRuleByCodeId(Long codeId) {
        return codeRuleMapper.selectByCodeId(codeId);
    }

}
